package com.jiangkai.controller.login;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.jiangkai.common.config.GlobalConstant;
import com.jiangkai.entity.system.SysUser;
import com.jiangkai.entity.vo.TreeMenu;
import com.jiangkai.model.system.ISysMenuService;
import com.jiangkai.utils.SpringUtil;

@Controller  

public class LoginController {
	 
	
	
	@Autowired 
	private ISysMenuService sysMenuService;
	/**
	 * 登录页面
	 */
    @RequestMapping("/login")
	public String login(HttpServletRequest request,Model model){
		return "login";
	}
	
	@ResponseBody
    @RequestMapping("/doLogin")  
    public Map login(HttpServletRequest request,Model model,String username,String password){  
 
		 Map<String,String> result = new HashMap<String,String>();
		if (SecurityUtils.getSubject().getSession() != null){
			SecurityUtils.getSubject().logout();
		}
		
		Subject subject = SecurityUtils.getSubject();
		subject.getPrincipal();
		// true：参数未改变，无需重新登录，默认为已经登录成功；false：需重新登录
		if(subject.isAuthenticated()){
			result.put("code", "200");
			return result;
		}
		// 组装 token，包括用户名、密码、角色、权限等等
		UsernamePasswordToken token = new UsernamePasswordToken(username,password);
		 token.setRememberMe(true);  
		// shiro 验证登录
		try {
			SecurityUtils.getSubject().login(token);
		} catch (UnknownAccountException ex){
			result.put("code", "201");
			result.put("msg", "帐号或密码错误");
			return result; 
		} catch (IncorrectCredentialsException ex){
			result.put("code", "201");
			result.put("msg", "帐号不存在或者密码错误");
		    return result; 
		} catch (AuthenticationException ex) {
			result.put("code", "201");
			result.put("msg", "未知异常");
		    return result; 
		} catch (Exception e) {
			result.put("code", "201");
			result.put("msg", "内部错误，请重新尝试");
		    return result; 
		}
		Subject subject2 = SecurityUtils.getSubject();
		subject2.getSession().setAttribute(GlobalConstant.SESSION_AUTH_LOGIN_USERNAME, username);
		if(subject2.isAuthenticated()){
				System.out.println("登陆成功");
		}
		if(subject2.isRemembered()){
			System.out.println("记住我成功");
		}
		
		result.put("code", "200");
		
		Session session = subject.getSession();
		SysUser sysUser = (SysUser) subject.getPrincipal();
		List<TreeMenu> treeMenus = sysMenuService.selectTreeMenuByUserId(sysUser.getId());
		Gson gson = new Gson();
		String gonss = gson.toJson(treeMenus);
		System.out.println(gonss);
		session.setAttribute("treeMenus", treeMenus);
		session.setAttribute("treeMenusJson", gonss);
		System.out.println("当前用户:"+subject.getPrincipal());
		//获取用户菜单
//		 SysUser sysUser = (SysUser) subject2.getPrincipal();
//		String currentUserId =  (String) SecurityUtils.getSubject().getSession().getAttribute("currentUserId");
//		List<TreeMenu> treeMenus = sysMenuService.selectTreeMenuByUserId(sysUser.getId());
//		request.setAttribute("treeMenus", treeMenus);
        return result;  
    }  
	
	
//	  @RequiresPermissions("courier:main")
	  @RequestMapping("/main")  
	    public String main(HttpServletRequest request,Model model){  
			Subject subject = SecurityUtils.getSubject();
//			Session session = subject.getSession();
//			SysUser sysUser = (SysUser) subject.getPrincipal();
//			List<TreeMenu> treeMenus = sysMenuService.selectTreeMenuByUserId(sysUser.getId());
//			Gson gson = new Gson();
//			String gonss = gson.toJson(treeMenus);
//			System.out.println(gonss);
//			session.setAttribute("treeMenus", treeMenus);
//			session.setAttribute("treeMenusJson", gonss);
//			System.out.println("当前用户:"+subject.getPrincipal());
			subject.isRemembered();
			System.out.println("是否记住："+subject.isRemembered());
        return "main/mainiframe";  
	    }  
	  
	  @RequestMapping("/secondmain")  
	    public String secondmain(HttpServletRequest request,Model model){  
			Subject subject = SecurityUtils.getSubject();
			System.out.println("当前用户:"+subject.getPrincipal());
			subject.isRemembered();
			System.out.println("是否记住："+subject.isRemembered());
      return "main/secondmain";  
	    }  
}
